import Singleton from "../base/Singleton";
import { NodePool, Prefab, Node, instantiate } from "cc";



/** 节点池 */
export class PoolManager  {
    // extends Singleton
    // static get Instance() {
    //     return super.GetInstance<PoolManager>();
    // }

    private _nodePools: Record<string, NodePool> = {}; // 节点池

    // 对外提供接口
    put(obj: Node) {
        if(!obj) {
            return
        }

        const key = obj.name;

        let pool = this._nodePools[key];
        if(!pool) {
            pool = new NodePool();
            this._nodePools[key] = pool;
        } else {
            pool.put(obj);
        }
    }

    get(pf: Prefab){
        if(!pf) {
            return
        }
        const key = pf.name;
        let target: Node;
        let pool = this._nodePools[key];
        if(!pool) {
            target = instantiate(pf);
            pool = new NodePool();
            this._nodePools[key] = pool;
        } else {
            if(pool.size() >0) {
                target = pool.get();
            } else {
                target = instantiate(pf);
            }
        }
        return target;
    }
    
}